import pandas as pd
import numpy as np

def read_data():
    # 第一章第一节
    df = pd.read_csv("data/train.csv")
    print(df)
    chunk = pd.read_csv("data/train.csv",chunksize=100)
    print(chunk)
    df.columns = ["乘客ID","是否幸存","乘客等级","乘客姓名","性别","年龄","堂兄弟/妹个数","父母与小孩个数","船票信息","票价","客舱","登船港口"]
    print(df)
    print(df.info())
    print(df.describe())
    print(df.head(10))
    print(df.tail(15))
    print(df.isnull())
    df.to_csv("data/train_chinese1.csv")
    pass
def get_data():
    sdata = {'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000}
    example_1 = pd.Series(sdata)
    print(example_1)
    data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada'],
            'year': [2000, 2001, 2002, 2001, 2002, 2003], 'pop': [1.5, 1.7, 3.6, 2.4, 2.9, 3.2]}
    example_2 = pd.DataFrame(data)
    print(example_2)
    df = pd.read_csv("data/train.csv")
    print(df.columns)
    print(df["Cabin"].head(3))
    test_1 = pd.read_csv("data/test_1.csv")
    print(test_1.head(3))
    print(df.head(3))
    del test_1["a"]
    print(test_1.head(3))
    print(df.shape)
    df.drop(['PassengerId','Name','Age','Ticket'],axis=1)
    print(df.head(3))
    print(df.shape)
    # 进行数据筛选
    d = df[df['Age']<10]
    print(d.head(3))
    midage = df[(df['Age']>10)&df['Age']<50]
    print(midage.head(3))
    midage = midage.reset_index(drop=True)
    print(midage.head(3))

    print(midage.loc[[100],['Pclass','Sex']])
    print(midage.loc[[100,105,108],['Pclass','Name','Sex']])
    print(midage.iloc[[100, 105, 108], [2, 3, 4]])
    pass
def clear_data():
    text = pd.read_csv('data/train_chinese.csv')
    print(text.head(3))
    frame = pd.DataFrame(np.arange(8).reshape((2, 4)),
                         index=['2', '1'],
                         columns=['d', 'a', 'b', 'c'])
    # 升序排列
    print(frame)
    frame.sort_values(by='c', ascending=True)
    print(frame)
    frame.sort_index()
    print(frame)
    frame.sort_index(axis=1)
    print(frame)
    frame.sort_index(axis=1, ascending=False)
    print(frame)
    # ac同时降序
    frame.sort_values(by=['a', 'c'], ascending=False)
    print(frame)
    text.sort_values(by=['票价', '年龄'], ascending=False).head(3)
    # 建立一个例子
    frame1_a = pd.DataFrame(np.arange(9.).reshape(3, 3),
                            columns=['a', 'b', 'c'],
                            index=['one', 'two', 'three'])
    frame1_b = pd.DataFrame(np.arange(12.).reshape(4, 3),
                            columns=['a', 'e', 'c'],
                            index=['first', 'one', 'two', 'second'])
    c = frame1_a + frame1_b
    print(c)
    d = max(text['兄弟姐妹个数'] + text['父母子女个数'])
    print(d)
    frame2 = pd.DataFrame([[1.4, np.nan],
                           [7.1, -4.5],
                           [np.nan, np.nan],
                           [0.75, -1.3]
                           ], index=['a', 'b', 'c', 'd'], columns=['one', 'two'])
    print(frame2)
    print(frame2.describe())
    print(text['票价'].describe())
    text['父母子女个数'].describe()
    pass
if __name__ == '__main__':
    clear_data()